package com.ma4567.remotecontroller.PermissionRequest;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.ma4567.remotecontroller.MysqlHelper.TableLogManager;
import com.ma4567.remotecontroller.MysqlHelper.TableRequestLog;
import com.ma4567.remotecontroller.RemoteControllerApplication;
import com.ma4567.remotecontroller.UserManager.UserPermission;
import com.ma4567.remotecontroller.ffmpegManager.ffmpegController;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;

import java.sql.SQLException;

//返回权限列表内容
class PermissionListReturnIndex extends UserPermission {
    public int Status;

    public PermissionListReturnIndex(int Status, UserPermission UserPermission) {
        super(UserPermission);
        this.Status = Status;
    }

    public PermissionListReturnIndex(int Status) {
        this.Status = Status;
        this.DeleteFile = false;
        this.DownloadFile = false;
        this.UploadFile = false;
        this.RemoteControl = false;
        this.Root = false;
    }
}

@CrossOrigin
@RestController
public class Permission {
    //return user function list
    @RequestMapping(value = "/api/allPermission", method = RequestMethod.GET)
    public String GetAllPermission(HttpServletRequest request,
                                   @RequestParam("cookie") String Cookie) throws SQLException {
        ffmpegController.StopStream();
        TableRequestLog.AddLog("Request permission list", Cookie, request.getRemoteAddr());
        UserPermission userPermission = RemoteControllerApplication.userManager.GetUserPermission(Cookie);

        if(userPermission == null){
            return JSONObject.toJSONString(new PermissionListReturnIndex(1), SerializerFeature.PrettyFormat);
        }

        TableLogManager.AddLog("Request permission list", Cookie);
        return JSONObject.toJSONString(new PermissionListReturnIndex(0, userPermission) , SerializerFeature.PrettyFormat);
    }
}
